﻿using CKS.PLC.Action.Subscription;
using CKS.PLC.Action.System;
using CKS.PLC.Core;
using FreeSql;
using Infrastructure;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using System.Text;
using ZR.Model.Models.ProductionControl;
using ZR.ServiceCore.Services;

namespace CKS.PLC.Action {
    /// <summary>
    /// 订阅动作基类
    /// 可根据不同项目自行定义ActionId
    /// </summary>
    public abstract partial class PLCAction : BasePlcAction {

        public PLCAction(DecodeValue value) : base(value) {

        }
        /// 如果多个站 每个站的每个DB文件的操作编号可以相同。
        /// 根据工站编号 - DB块 - 操作编号类
        /// 程序目录示例： OP50/DB3011/CKS01Action.cs、OP60/DB3010/CKS01Action.cs
        //  流程action 1 - 100 
        //              1 请求加工 2请求保存    
        //  纯数据 101 - 200

        /*
         插件式：每次执行前检查DLL是否发生变化，
            1.有变化则更新缓存程序集
            2.从缓存程序集提取实例执行
         */

        /*
         *Old EffectType:
                     1 订阅数据、2 MES心跳、3 PLC心跳、4 实时采集 5 单表数据追溯
                     6 订阅+保存结果、7 PLC确认、8 上传工艺参数、9 请求加工
         */

        public static PLCAction GetInstance(DecodeValue value) {
            PLCAction pLCAction = default;

            switch (value.Position.EffectType) {
                case ZR.Model.Enums.eEffectType.RealTime_Subscription://实时采集+订阅数据
                case ZR.Model.Enums.eEffectType.Subscription_Data://订阅数据
                    pLCAction = new SubscriptionDataAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.SaveData_Request://数据保存请求
                    pLCAction = new SaveProcessingResultsAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.Technique_Confirmation://工参确认
                    pLCAction = new TechniqueConfirmAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.Technique_Upload://工参上传
                    pLCAction = new TechniqueUploadAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.Processing_Request://加工请求
                    pLCAction = new StartWorkingAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.Alarm_Request://报警请求
                    pLCAction = new SaveAlarmAction(value);
                    break;
                case ZR.Model.Enums.eEffectType.OEE_Statistics://OEE统计
                    pLCAction = new OEEStatisticsAction(value);
                    break;
                default:
                    //throw new NotImplementedException("当前点位设置的作用类型暂未实现Action方法");
                    break;
            }

            //switch (position.PlcId)
            //{
            //    //订阅点需手动补充逻辑
            //    default:
            //        throw new InvalidOperationException("当前工位编号逻辑尚未添加");
            //}

            return pLCAction;
        }
    }
}
